<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header height="70px">
      <div class="info">
        <img src="../assets/head.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <!-- 中间区域 -->
    <el-container>
      <!-- 中间的左边导航栏 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <!-- 左侧展开按钮 -->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
        
        :default-active="activePath" 
          router
          :collapse="iscollapse"
          :collapse-transition="false"
          unique-opened="true"
          background-color="rgb(49, 55, 67)"
          text-color="#fff"
          active-text-color="lightblue"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span> {{ item.authName }} </span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="savaNavState('/' + subItem.path)"
            >
              <i class="el-icon-menu"></i>
              <span> {{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 中间的右边 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

  <!-- 退出按钮 -->
</template>


<script>
export default {
  data() {
    return {
      menulist: [], //菜单栏列表
      iconObj: {
        //菜单栏图标对象
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      iscollapse: false,
      //动态绑定的激活菜单的地址
      activePath:'',
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有菜单栏
    async getMenuList() {
      const { data: res } = await this.$http.get("/menus");
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    //点击按钮 收缩菜单栏
    toggleCollapse() {
      this.iscollapse = !this.iscollapse;
    },
    savaNavState(activePath){
      window.sessionStorage.setItem('activePath',activePath)
      this.activePath = activePath
    }
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  background-color: rgb(54, 61, 64);
  padding-left: 0;
  align-items: center;
  div {
    display: flex;
    align-items: center;
    font-size: 20px;
    color: #fff;
  }
  img {
    height: 80%;
    border-radius: 50%;
    margin-right: 10px;
  }
}
.el-aside {
  background-color: rgb(49, 55, 67);
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  text-align: center;
  font-size: 10px;
  color: #fff;
  line-height: 24px;
  background-color: rbg(71, 81, 99);
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>